package com.thti.mecool.client.model.biz;

import java.util.List;

import android.content.Context;
import android.database.Cursor;

import com.thti.mecool.client.db.DataBase;
import com.thti.mecool.client.model.po.Project;
import com.thti.mecool.client.model.po.Task;

public class ProjectBizImpl {
	
	private DataBase dataBase;

	public ProjectBizImpl(Context context) {
		dataBase = new DataBase(context);
	}
	public void save(Project project){
		String sqlString="insert into project(userId,projectId,projectName,planId,isType,createTime) values(?,?,?,?,?,?)";
		dataBase.execObject(sqlString, new String[]{project.getUserId(),project.getProjectId(),project.getProjectName(),project.getPlanId(),String.valueOf(project.getIsType()),String.valueOf(project.getCreateTime())});
		dataBase.close();
	}
	
	public void update(Project project){
		String sqlString="update project set projectName=? where planId=? and projectId=?";
		dataBase.execObject(sqlString, new String[]{project.getProjectName(),project.getPlanId(),project.getProjectId()});
		dataBase.close();
	} 
	
	public void delete(String planId,String projectId){
		String sqlString="delete from  project where planId=? and projectId=?";
		dataBase.execObject(sqlString, new String[]{planId,projectId});
		dataBase.close();
	}
	
	public int findPlanId(){
		String sqlString="select  * from project order by createTime DESC  limit 0,1";
		Cursor cursor=dataBase.queryObject(sqlString, new String[]{});
		int i=0;
		while(cursor.moveToNext()){
		  i=	cursor.getInt(cursor.getColumnIndex("id"));
		}
		cursor.close();
		dataBase.close();
		return i;
	} 
	
	public String findProjectNameByProjectId(String projectId){
		String sqlString="select  * from project where projectId=?";
		Cursor cursor=dataBase.queryObject(sqlString, new String[]{projectId});
		String projectName=null;
		while(cursor.moveToNext()){
			projectName=	cursor.getString(cursor.getColumnIndex("projectName"));
		}
		cursor.close();
		dataBase.close();
		return projectName;
	} 
	
	
	/**
	 * 判断projrctId是已经存在
	 * @param id
	 * @return
	 */
	public boolean findProjectId(String id,String  planId){
		String sqlString="select  * from project where projectId=? and planId=?";
		Cursor cursor=dataBase.queryObject(sqlString, new String[]{id,planId});
		if(cursor.moveToNext()){
			boolean flag = cursor.getLong(0)>0 ?true:false;
			cursor.close();
			dataBase.close();
			return flag;
		}
		cursor.close();
		dataBase.close();
		return false;

	} 
	
	public Project findByPlanId(int id){
		String sqlString="select pro.planId,pro.userId,pro.projectName,t.taskId,t.taskName,t.storeId,t.storeName,t.startTime,t.endTime from project pro,task twhere pro.planId=? AND pro.id=t.projectId";
		Cursor cursor=dataBase.queryObject(sqlString, new String[]{});
		Project project=new Project();
		while(cursor.moveToNext()){	
			//project.setId(cursor.getInt(cursor.getColumnIndex("id")));
			project.setPlanId(cursor.getString(cursor.getColumnIndex("planId")));
			project.setProjectName(cursor.getString(cursor.getColumnIndex("projectName")));
			project.setUserId(cursor.getString(cursor.getColumnIndex("userId")));
			Task task=new Task();
			task.setTaskId(cursor.getString(cursor.getColumnIndex("taskId")));
			task.setTaskName(cursor.getString(cursor.getColumnIndex("taskName")));
			task.setStoreId(cursor.getString(cursor.getColumnIndex("storeId")));
			task.setStoreName(cursor.getString(cursor.getColumnIndex("storeName")));
			task.setEndTime(cursor.getString(cursor.getColumnIndex("endTime")));
			task.setStartTime(cursor.getString(cursor.getColumnIndex("startTime")));
			project.setTask(task);
		}
		cursor.close();
		dataBase.close();
		return project;
	}
	
	@SuppressWarnings("unchecked")
	public List<Project> findAll(String planId){
		// select pro.planId,p.planName,p.userId,pro.projectName,t.taskId,t.taskName,t.storeId,t.storeName,t.startTime,t.endTime from plan p,project pro,task t where p.id=pro.planId AND pro.id=t.projectId
		String sql="select  * from  project  where planId=?";
		List<Project> list=dataBase.findAll(Project.class, sql, new String[]{planId});
		dataBase.close();
		return list;
		
	}

	public void delete(String planId){
		String sqlString="delete from  project where planId=?";
		dataBase.execObject(sqlString, new String[]{planId});
		dataBase.close();
	}
}
